package com.changpei.core.config;

import com.alibaba.fastjson.JSONObject;
import com.changpei.common.cache.RedisUtil;
import com.changpei.common.cache.enums.CacheCode;
import com.changpei.common.utils.JsonUtil;
import com.changpei.core.pojo.*;
import com.changpei.core.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 *
 * 凌晨一点执行备份
 * @author Way on 2021/9/26
 */
@Component
public class SyncData {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private FansService fansService;

    @Autowired
    private UserService userService;

    @Autowired
    private FollowService followService;

    @Autowired
    private FavoritesService favoritesService;

    @Autowired
    private PraiseService praiseService;

    @Autowired
    private ContentService contentService;

    @Autowired
    private CommentService commentService;

    /**
     * 用户粉丝
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void fans() {
        Map<Object, Object> map = redisUtil.hGet(CacheCode.USER_FANS_KEY.getValue());
        if (!ObjectUtils.isEmpty(map)) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Long userId = Long.parseLong(entry.getKey().toString());
                List<Long> fanIds = JSONObject.parseArray(entry.getValue().toString(), Long.class);
                for (Long fanId : fanIds) {
                    Fans fans = new Fans();
                    fans.setFansId(fanId);
                    fans.setUserId(userId);
                    fansService.save(fans);
                }

                // 维护粉丝数
                User user = userService.getById(userId);
                user.setFansNum(Long.parseLong(String.valueOf(fanIds.size())));
                userService.updateById(user);
            }
        }
    }

    /**
     * 用户关注
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void follow() {
        Map<Object, Object> map = redisUtil.hGet(CacheCode.USER_FOLLOW_KEY.getValue());
        if (!ObjectUtils.isEmpty(map)) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Long userId = Long.parseLong(entry.getKey().toString());
                List<Long> followIds = JSONObject.parseArray(entry.getValue().toString(), Long.class);
                for (Long followId : followIds) {
                    Follow follow = new Follow();
                    follow.setFollowId(followId);
                    follow.setUserId(userId);
                    followService.save(follow);
                }

                // 维护关注数
                User user = userService.getById(userId);
                user.setFollowedNum(Long.parseLong(String.valueOf(followIds.size())));
                userService.updateById(user);
            }
        }
    }

    /**
     * 内容收藏
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void contentCollect() {
        Set<Object> set = redisUtil.sGet(CacheCode.CONTENT_COLLECT.getValue());
        if (!ObjectUtils.isEmpty(set)) {
            for (Object o : set) {
                Favorites favorites = JsonUtil.jsonToBean(o, Favorites.class);
                favoritesService.save(favorites);
            }
        }
    }

    /**
     * 内容点赞
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void contentLike() {
        Set<Object> set = redisUtil.sGet(CacheCode.CONTENT_LIKE.getValue());
        if (!ObjectUtils.isEmpty(set)) {
            for (Object o : set) {
                Praise praise = JsonUtil.jsonToBean(o, Praise.class);
                praiseService.save(praise);
            }
        }
    }

    /**
     * 更新收藏数
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void contentCollectCount() {
        Map<Object, Object> map = redisUtil.hGet(CacheCode.CONTENT_COLLECT_COUNT.getValue());
        if (!ObjectUtils.isEmpty(map)) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Long contentId = Long.parseLong(entry.getKey().toString());
                Content content = contentService.getById(contentId);
                content.setCollectedNum(Long.parseLong(entry.getValue().toString()));
                contentService.updateById(content);
            }
        }
    }

    /**
     * 内容点赞数
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void contentLikeCount() {
        Map<Object, Object> map = redisUtil.hGet(CacheCode.CONTENT_LIKE_COUNT.getValue());
        if (!ObjectUtils.isEmpty(map)) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Long contentId = Long.parseLong(entry.getKey().toString());
                Content content = contentService.getById(contentId);
                content.setLikedNum(Long.parseLong(entry.getValue().toString()));
                contentService.updateById(content);
            }
        }
    }

    /**
     * 备份评论及评论数
     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void comment() {
        Map<Object, Object> map = redisUtil.hGet(CacheCode.COMMENT.getValue());
        if (!ObjectUtils.isEmpty(map)) {
            for (Map.Entry<Object, Object> entry : map.entrySet()) {
                Long contentId = Long.parseLong(entry.getKey().toString());
                List<Comment> comments = JSONObject.parseArray(entry.getValue().toString(), Comment.class);
                commentService.saveBatch(comments);

                Content content = contentService.getById(contentId);
                content.setCommentedNum(Long.parseLong(String.valueOf(comments.size())));
                contentService.updateById(content);
            }
        }
    }
}
